package com.alibaba.citrus.cr.mn.order.center.forecastorder.convert;

import cn.hutool.core.date.DateUtil;
import com.alibaba.citrus.cr.common.enums.ordercenter.OrderBizTagEnum;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.mn.order.center.common.util.AssertUtils;
import com.epoch.app.crforecastsales.bo.ForecastOrderBO;
import com.epoch.app.mnordercenter.model.dto.CreateForecastDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class CreateForecastDTOToForecastOrderBOConvertor {
    public static ForecastOrderBO convert(CreateForecastDTO createForecastDTO) {
        ForecastOrderBO forecastOrderBO = new ForecastOrderBO();
        forecastOrderBO.setOuterOrderId(createForecastDTO.getOuterOrderId());
        forecastOrderBO.setSourceOrderNo(createForecastDTO.getSourceOrderNo());
        forecastOrderBO.setSubmitFlag(createForecastDTO.getSubmitFlag());
        forecastOrderBO.setOrderTypeCode(createForecastDTO.getSalesOrderType());
        forecastOrderBO.setTransportModeCode(createForecastDTO.getDeliveryMethodCode());
        forecastOrderBO.setCustomerExpectTDate(createForecastDTO.getExpectedDeliveryDate());
        forecastOrderBO.setShipToPartyCode(createForecastDTO.getShipToPartyCode());
        forecastOrderBO.setSoldToPartyCode(createForecastDTO.getCustomerCode());
        forecastOrderBO.setProductGroupCode(createForecastDTO.getProductGroupCode());
        forecastOrderBO.setChannelCode(createForecastDTO.getBusinessChannel());
        forecastOrderBO.setSalesChannelCode(createForecastDTO.getSalesChannelCode());
        forecastOrderBO.setSalesChannelName(createForecastDTO.getSalesChannelName());
        forecastOrderBO.setSalesDepartmentCode(createForecastDTO.getSalesDepartmentCode());
        forecastOrderBO.setSalesDepartmentName(createForecastDTO.getSalesDepartmentName());
        forecastOrderBO.setCreateReasonCode(createForecastDTO.getOrderReasonCode());
        forecastOrderBO.setProductGroupCode(createForecastDTO.getProductGroupCode());
        forecastOrderBO.setShippingWarehouseCode(createForecastDTO.getShippingWarehouseCode());
        forecastOrderBO.setDeliveryFactoryCode(createForecastDTO.getDeliveryFactoryCode());
        forecastOrderBO.setStockCode(createForecastDTO.getStockCode());
        forecastOrderBO.setCustomerPurchaseOrderNo(createForecastDTO.getCustomerPurchaseOrderNo());
        forecastOrderBO.setCustomerPurchaseDate(createForecastDTO.getCustomerPurchaseDate());
        forecastOrderBO.setRemark(createForecastDTO.getNote());
        forecastOrderBO.setSalesOrganizationCode(createForecastDTO.getSalesOrganizationCode());
        forecastOrderBO.setSalesOrganizationCode(createForecastDTO.getSalesOrganizationCode());
        forecastOrderBO.setBizFeatures(createForecastDTO.getBizFeatures());
        forecastOrderBO.setCreatorCode(createForecastDTO.getCreatorCode());

        // 一盘货新增字段
        forecastOrderBO.setOrderCategoryCode(createForecastDTO.getOrderCategoryCode()); // 订单类别编码
        forecastOrderBO.setOrderChannelCode(createForecastDTO.getOrderChannelCode()); // 订单渠道编码
        forecastOrderBO.setReceiveWarehouseCode(createForecastDTO.getReceiveWarehouseCode()); // 收货逻辑仓库编码
        forecastOrderBO.setReceiveFactoryCode(createForecastDTO.getReceiveFactoryCode()); // 收货仓库编码
        forecastOrderBO.setReceiveStockCode(createForecastDTO.getReceiveStockCode()); // 收货库存地点编码
        forecastOrderBO.setFulfillmentTypeCode(createForecastDTO.getFulfillmentTypeCode()); // 履约方式编码
        forecastOrderBO.setFulfillmentPriority(createForecastDTO.getFulfillmentPriority()); // 履约优先级
        if(CollectionUtils.isNotEmpty(createForecastDTO.getOrderBizTag())){
            AssertUtils.isTrue(createForecastDTO.getOrderBizTag().size() > 10, "OC-01-001-01-15-004", "订单标签列表", createForecastDTO.getOrderBizTag().size(), 10);

            List<String> sameOrderBizTagList = createForecastDTO.getOrderBizTag().stream().collect(Collectors.toMap(p -> p, p -> 1, Integer::sum))
                    .entrySet().stream().filter(entry -> entry.getValue() > 1)
                    .map(Map.Entry::getKey).collect(Collectors.toList());
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(sameOrderBizTagList)) {
                throw new FunctionException("OC-01-001-01-15-002", "订单标签", StringUtils.join(sameOrderBizTagList, ","));
            }
            for(String orderTag : createForecastDTO.getOrderBizTag()) {
                AssertUtils.lengthCheck(orderTag, 50, false, "OC-01-001-01-15-004", "订单标签");
                // todo:先用枚举校验，后面替换成字典校验
                // 校验订单标签
                OrderBizTagEnum orderBizTagEnum = OrderBizTagEnum.of(orderTag);
                Optional.ofNullable(orderBizTagEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单标签"));
            }
            String orderBizTag = createForecastDTO.getOrderBizTag().stream().collect(Collectors.joining(","));
            forecastOrderBO.setOrderTag(orderBizTag); // 订单标签
        }
        forecastOrderBO.setAutoFulfillment(createForecastDTO.getAutoFulfillment()); // 是否自动履约
        if (StringUtils.isNotBlank(createForecastDTO.getValidBeginDate())) {
            AssertUtils.lengthCheck(createForecastDTO.getValidBeginDate(), 10, false, "OC-01-001-01-15-004", "生效开始日期");
            AssertUtils.isTrue(!DateUtils.isDateFormat(createForecastDTO.getValidBeginDate()), "OC-00-001-01-15-001", "生效开始日期");
            forecastOrderBO.setVaildBeginTime(DateUtil.parseDate(createForecastDTO.getValidBeginDate())); // 生效开始时间
        }
        if (StringUtils.isNotBlank(createForecastDTO.getValidEndTime())) {
            AssertUtils.lengthCheck(createForecastDTO.getValidEndTime(), 19, false, "OC-01-001-01-15-004", "有效期结束时间");
            AssertUtils.isTrue(!DateUtils.isDateTimeFormat(createForecastDTO.getValidEndTime()), "OC-00-001-01-15-001", "有效期结束时间");
            forecastOrderBO.setValidEndTime(DateUtil.parseDateTime(createForecastDTO.getValidEndTime())); // 有效期结束时间
        }
        forecastOrderBO.setKaSystem(createForecastDTO.getKaSystem()); // KA系统
        forecastOrderBO.setKaArea(createForecastDTO.getKaArea()); // KA大区
        forecastOrderBO.setSourceOrderDate(createForecastDTO.getSourceOrderTime()); // 来单日期
        forecastOrderBO.setCustomerGrade(createForecastDTO.getCustomerGrade()); // 客户等级
        forecastOrderBO.setCustomerGroup(createForecastDTO.getCustomerGroup()); // 客户组
        return forecastOrderBO;
    }
}
